Getting Excel Data from Another Sheet
In Excel, getting data from another sheet is a common task that can be achieved in several ways. In this article, we will explore the different methods to obtain data from another sheet, along with some tips and tricks to make the process easier.
Method 1: Using the _sheet_from_only_macro function
The sheet_from_only_macro function is a powerful tool that allows you to get data from a sheet without formatting or merging. Here’s how to use it:
- Go to the Developer tab in the Excel ribbon
- Click on "Macro" in the "Code" group
- Select "Sheet From Only Macro" from the dropdown menu
-
In the code editor, paste the following macro:
Function SheetFromOnlyMacro(sht As Worksheet, chrNum As Char, opt As Boolean) As Variant
Dim i As Integer
Dim data As Variant
On Error Resume Next
Set data = shtcells(chrNum)
On Error GoTo 0
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
If opt Then
data = datauppercase
Else
data = datalower
End If
data = Split(data, "|")
data = Split(data, "&")
For Each cell In data
data(i) = cell
i = i + 1
Next cell
data = Join(data, "&")
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
data = Split(data, "|")
data = Split(data, "&")
data = Join(data, "|")
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
Set sht = sht
Function EndSheetFromOnlyMacro()
' Purpose: Get all data from a sheet in a macro
' Step 1: Get data from the source sheet
data = shtcells(1, 1, 1)
' Step 2: Split data into individual cells
For Each cell In data
If cell <> "" Then
data(i) = cell
i = i + 1
End If
Next cell
' Step 3: Split data into individual data points
For Each cell In data
data(i) = cell
i = i + 1
Next cell
' Step 4: Remove commas and periods from data points
data = Replace(data, ",", "")
data = Replace(data, ".", "")
' Step 5: Remove quotes and commas from data points
data = Replace(data, """, "")
data = Replace(data, ",", "")
data = Replace(data, ".", "")
' Step 6: Split data into individual fields
data = Split(data, "|")
' Step 7: Convert data to string format
For Each field In data
field = fieldtrim
Next field
' Step 8: Use the **Getdata** function to format and return data
sht = sht_getdata(1, 1, 1, data)
' Step 9: Get data from the data returned by Getdata function
Set data = shtcells(1, 1, 1)
' Step 10: If opt is true, then upcase data
If opt Then
data = datauppercase
Else
data = datalower
End If
' Step 11: If error occurred, return an error message
If Not IsError(data) Then
sht = sht_uierror()
Exit Function
End If
' Step 12: Set sht to the original source sheet
sht = sht
' Function EndSheetFromOnlyMacro
End Function
EndMacro - This macro uses the SheetFromOnlyMacro function to get all data from a sheet.
- It splits the data into individual cells, data points, and fields.
- It uses the Getdata function to format and return the data.
- If the SheetFromOnlyMacro function encounters an error, it returns an error message.
- You can modify the macro to suit your needs by changing the data source and formatting options.
Method 2: Using the **C店FromSheet**** function
The C# FromSheet function is another powerful tool that allows you to get data from a sheet. Here’s how to use it:
- Go to the Developer tab in the Excel ribbon
- Click on "Tools" in the "Options" group
- Click on " Add-ins"
- Search for "VBA macros" and click on the VBA Editor**
- In the VBA Editor, open the "Module1" module (or any other module that you want to use)
-
Paste the following code:
Function C#FromSheet(sht As Worksheet, chrNum As Char, opt As Boolean) As Variant
Dim data As Variant
On Error Resume Next
data = sht cells(chrNum).Value
On Error GoTo 0
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
If opt Then
data = datauppercase
Else
data = datalower
End If
data = Split(data, "|")
data = Split(data, "&")
For Each cell In data
data(i) = cell
i = i + 1
Next cell
data = Join(data, "&")
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
data = Split(data, "|")
data = Split(data, "&")
data = Join(data, "|")
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
Set sht = sht
Function EndSheetFromSheet()
' Purpose: Get all data from a sheet in a macro
' Step 1: Get data from the source sheet
data = sht cells(1, 1, 1).Value
' Step 2: Split data into individual cells
For Each cell In data
If cell <> "" Then
data(i) = cell
i = i + 1
End If
Next cell
' Step 3: Split data into individual data points
For Each cell In data
data(i) = cell
i = i + 1
Next cell
' Step 4: Remove commas and periods from data points
data = Replace(data, ",", "")
data = Replace(data, ".", "")
' Step 5: Remove quotes and commas from data points
data = Replace(data, """, "")
data = Replace(data, ",", "")
data = Replace(data, ".", "")
' Step 6: Split data into individual fields
data = Split(data, "|")
' Step 7: Convert data to string format
For Each field In data
field = fieldtrim
Next field
' Step 8: Use the **Getdata** function to format and return data
sht = sht_getdata(1, 1, 1, data)
' Step 9: Get data from the data returned by Getdata function
Set data = shtcells(1, 1, 1).Value
' Step 10: If opt is true, then upcase data
If opt Then
data = datauppercase
Else
data = datalower
End If
' Step 11: If error occurred, return an error message
If Not IsError(data) Then
sht = sht_uierror()
Exit Function
End If
' Step 12: Set sht to the original source sheet
sht = sht
' Function EndSheetFromSheet
End Function
EndModule - This macro uses the C# FromSheet function to get all data from a sheet.
- It splits the data into individual cells, data points, and fields.
- It uses the Getdata function to format and return the data.
- If the C# FromSheet function encounters an error, it returns an error message.
- You can modify the macro to suit your needs by changing the data source and formatting options.
Method 3: Using the _archivogroup_fromonlymacro function
The archivogroup_fromonlymacro function is another powerful tool that allows you to get data from a sheet. Here’s how to use it:
- Go to the Developer tab in the Excel ribbon
- Click on "Macros" in the "Code" group
- Select "archivogroup_fromonlymacro" from the dropdown menu
-
In the code editor, paste the following macro:
Function ArchivogroupFromOnlyMacro(sht As Worksheet, chrNum As Char, opt As Boolean) As Variant
Dim data As Variant
On Error Resume Next
data = shtarchive(chrNum).Value
On Error GoTo 0
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
If opt Then
data = datauppercase
Else
data = datalower
End If
data = Split(data, "|")
data = Split(data, "&")
For Each cell In data
data(i) = cell
i = i + 1
Next cell
data = Join(data, "&")
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
data = Split(data, "|")
data = Split(data, "&")
data = Join(data, "|")
data = Replace(data, "|", "\")
data = Replace(data, ",", "\")
data = Replace(data, "/", "\")
Set sht = sht
Function EndArchivogroupFromOnlyMacro()
' Purpose: Get all data from a sheet in a macro
' Step 1: Get data from the source sheet
data = shtarchive(1, 1, 1).Value
' Step 2: Split data into individual cells
For Each cell In data
If cell <> "" Then
data(i) = cell
i = i + 1
End If
Next cell
' Step 3: Split data into individual data points
For Each cell In data
data(i) = cell
i = i + 1
Next cell
' Step 4: Remove commas and periods from data points
data = Replace(data, ",", "")
data = Replace(data, ".", "")
' Step 5: Remove quotes and commas from data points
data = Replace(data, """, "")
data = Replace(data, ",", "")
data = Replace(data, ".", "")
' Step 6: Split data into individual fields
data = Split(data, "|")
' Step 7: Convert data to string format
For Each field In data
field = fieldtrim
Next field
' Step 8: Use the **Getdata** function to format and return data
sht = sht_getdata(1, 1, 1, data)
' Step 9: Get data from the data returned by Getdata function
Set data = shtarchive(1, 1, 1).Value
' Step 10: If opt is true, then upcase data
If opt Then
data = datauppercase
Else
data = datalower
End If
' Step 11: If error occurred, return an error message
If Not IsError(data) Then
sht = sht_uierror()
Exit Function
End If
' Step 12: Set sht to the original source sheet
sht = sht
' Function EndArchivogroupFromOnlyMacro
End Function
EndModule - This macro uses the archivogroup_fromonlymacro function to get all data from a sheet.
- It splits the data into individual cells, data points, and fields.
- It uses the Getdata function to format and return the data.
- If the archivogroup_fromonlymacro function encounters an error, it returns an error message.
- You can modify the macro to suit your needs by changing the data source and formatting options.
Tips and Tricks
- Always check the source sheet for errors before proceeding with the macro.
- Use the Save As option to save the macro as a new module, so you can import it into other sheets.
- Use the Exit Function statement to exit the macro immediately if an error occurs.
- Use the On Error GoTo 0 statement to clear the error message and prevent it from being displayed.
- Use the IsError function to check if an error occurred, and avoid using Not IsError.
- Use the datauppercase and datalower functions to convert data to uppercase and lowercase, respectively.
By following these steps and tips, you can easily get Excel data from another sheet using the SheetFromOnlyMacro, C# FromSheet, and archivogroup_fromonlymacro functions. Remember to always check the source sheet for errors and use the Save As option to save the macro as a new module.
