Tuesday, August 22, 2023

How to export and save each worksheet as separate new workbook in Excel?

 

Export and save worksheets as new workbook with VBA code

This following code will export every visible worksheet in a new workbook and save the workbook with the name of the original sheet in a newly created folder in the same path as the active workbook. Please do as following steps:

Step 1: Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

Step 2: Click Insert > Module, and paste the following macro in the Module Window:

VBA : Export and save worksheets as new workbook in a new folder.


Sub SplitWorkbook()
'Updateby20200806
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim xNWb As Workbook
Dim FolderName As String
Application.ScreenUpdating = False
Set xWb = Application.ThisWorkbook

DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = xWb.Path & "\" & xWb.Name & " " & DateString

If Val(Application.Version) < 12 Then
    FileExtStr = ".xls": FileFormatNum = -4143
Else
    Select Case xWb.FileFormat
        Case 51:
            FileExtStr = ".xlsx": FileFormatNum = 51
        Case 52:
            If Application.ActiveWorkbook.HasVBProject Then
                FileExtStr = ".xlsm": FileFormatNum = 52
            Else
                FileExtStr = ".xlsx": FileFormatNum = 51
            End If
        Case 56:
            FileExtStr = ".xls": FileFormatNum = 56
        Case Else:
            FileExtStr = ".xlsb": FileFormatNum = 50
        End Select
End If

MkDir FolderName

For Each xWs In xWb.Worksheets
On Error GoTo NErro
    If xWs.Visible = xlSheetVisible Then
    xWs.Select
    xWs.Copy
    xFile = FolderName & "\" & xWs.Name & FileExtStr
    Set xNWb = Application.Workbooks.Item(Application.Workbooks.Count)
    xNWb.SaveAs xFile, FileFormat:=FileFormatNum
    xNWb.Close False, xFile
    End If
NErro:
    xWb.Activate
Next

    MsgBox "You can find the files in " & FolderName
    Application.ScreenUpdating = True
End Sub

Step 3: Press the F5 key to run this code. And a prompt box will pop up to tell you the location of the new exported workbooks, and all of the worksheets of the original workbook have been exported to some new separate workbooks which named original sheets in a new specific folder. 

Monday, December 28, 2020

Android Studio CheckBox Save and Get Data Dari Database


1. DI bagian Layout siapkan check box yang akan di gunakan misalkan saya ingin memakai kategori dalam sebuah teknisi

layout.xml

<android.support.design.widget.TextInputLayout
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:layout_marginTop="15dp">

    <
TextView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="Pilih Kategori"
       
android:textColor="#000000"
       
android:textSize="16dp"
       
android:singleLine="true"
       
android:inputType="text"/>

    <
CheckBox
       
android:id="@+id/cLaptop"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Laptop" />

    <
CheckBox
       
android:id="@+id/cHandphone"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Handphone" />

    <
CheckBox
       
android:id="@+id/cLcd"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="LCD Monitor" />

    <
CheckBox
       
android:id="@+id/cKomputer"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Komputer" />

    <
CheckBox
       
android:id="@+id/cPrinter"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Printer" />

    <
CheckBox
       
android:id="@+id/cMotherboard"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Motherboard" />

    <
CheckBox
       
android:id="@+id/cHardisk"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Hardisk" />

    <
CheckBox
       
android:id="@+id/cSpeaker"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Speaker" />

</
android.support.design.widget.TextInputLayout>

 

2. Deklarasi ID dan Deklarasi Variabel pada .Java

Deklarasi Golbal

CheckBox cLaptop;
CheckBox
cHandphone;
CheckBox
cLcd;
CheckBox
cKomputer;
CheckBox
cPrinter;
CheckBox
cMotherboard;
CheckBox
cHardisk;
CheckBox
cSpeaker;

 

Di dalam Activity

public void onCreate(Bundle savedInstanceState) {
  
super.onCreate(savedInstanceState);
   setContentView(R.layout.
registrasi_teknisi);

  
cLaptop= (CheckBox) findViewById(R.id.cLaptop);
  
cHandphone= (CheckBox) findViewById(R.id.cHandphone);
  
cLcd= (CheckBox) findViewById(R.id.cLcd);
  
cKomputer= (CheckBox) findViewById(R.id.cKomputer);
  
cPrinter= (CheckBox) findViewById(R.id.cPrinter);
  
cMotherboard= (CheckBox) findViewById(R.id.cMotherboard);
  
cHardisk= (CheckBox) findViewById(R.id.cHardisk);
  
cSpeaker= (CheckBox) findViewById(R.id.cSpeaker);

}

 

3. Di bagian void save atau fungsi simpan untuk mengambil apa saja yang tadi

di pilih maka tambah kan ini untuk mendapatkan apa saja yang di centang

 

String pilKat1,pilKat2,pilKat3,pilKat4,pilKat5,pilKat6,pilKat7, pilKat8;

if(cLaptop.isChecked()){
  
pilKat1 = "Laptop,";
}
else if(!cLaptop.isChecked()){
  
pilKat1 = "";
}
if(cHandphone.isChecked()){
  
pilKat2 = "Handphone,";
}
else if(!cHandphone.isChecked()){
  
pilKat2 = "";
}
if(cLcd.isChecked()){
  
pilKat3 = "LCD Monitor,";
}
else if(!cLcd.isChecked()){
  
pilKat3 = "";
}
if(cKomputer.isChecked()){
  
pilKat4 = "Komputer,";
}
else if(!cKomputer.isChecked()){
  
pilKat4 = "";
}
if(cPrinter.isChecked()){
  
pilKat5 = "Printer,";
}
else if(!cPrinter.isChecked()){
  
pilKat5 = "";
}
if(cMotherboard.isChecked()){
  
pilKat6 = "Motherboard,";
}
else if(!cMotherboard.isChecked()){
  
pilKat6 = "";
}
if(cHardisk.isChecked()){
  
pilKat7 = "Hardisk,";
}
else if(!cHardisk.isChecked()){
  
pilKat7 = "";
}
if(cSpeaker.isChecked()){
  
pilKat8 = "Speaker,";
}
else if(!cSpeaker.isChecked()){
  
pilKat8 = "";
}

String lkategori=
pilKat1+pilKat2+pilKat3+pilKat4+pilKat5+pilKat6+pilKat7+pilKat8;

 

Nah para String lkategori ini lah hasilnya yang akan di kirim ke database

Contoh hasil yang di dapat beberapa yng di Ceklist (Laptop, Komputer, Printer,)

 

4. Maka Pasa saat save ke mysql kita kurangin 1 huruf di belakang guna untuk

Koma pada terakhir akan hilang menjadi (Laptop, Komputer, Printer)

Script Pada PHP untuk mengurangi 1 huruf di belakang:

$id_teknisi = $_POST['id_teknisi'];
$nama_teknisi = $_POST[
'nama_teknisi'];
$kategori = $_POST[
'kategori'];


if(strlen($kategori)>3){
   $kategori=substr($kategori,
0,strlen($kategori)-1);

}

$sql="INSERT INTO `$tbteknisi`
(`id_teknisi`, `nama_teknisi`, `kategori`) VALUES
(
'$id_teknisi', '$nama_teknisi', '$kategori')";
$simpan=process($conn,$sql);

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Oke Step Selanjutnya ya itu pada saat GET atau mengambil data dari data base

yang sudah di insert pada step di atas

 

1. Lakukan Step 1,2 pada step2 yang sudah ada di atas.

2. Pada saat menampilkan data atau mengambil data yang sudah di input kan maka jadikan

aray terlebih dahulu seperti berikut

 

String kategori="Laptop,LCD Monitor,Printer";

String[]ar=kategori.split(",");

  
if (Arrays.asList(ar).contains("Laptop")) {
     
cLaptop.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("Handphone")) {
     
cHandphone.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("LCD Monitor")) {
     
cLcd.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("Komputer")) {
     
cKomputer.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("Printer")) {
     
cPrinter.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("Motherboard")) {
     
cMotherboard.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("Hardisk")) {
     
cHardisk.setChecked(true);
   }
  
if (Arrays.asList(ar).contains("Speaker")) {
     
cSpeaker.setChecked(true);
   }

 

Lalu untuk mengambil data lagi Lakukan Step 3 dan 4 Pada langkah Yang pertama seperti di atas

 

Terimakasih, Silahkan Mencoba guys. :)