Hello, I have the following macro embedded in a button on one sheet that copies the data on that sheet. But I'd like to place the button on another sheet whilst maintaining the same function. I'm using the following code for the button. What should I change to make it reference a particular sheet instead of the active one?
PS: The original excel file has too many sensitive data so sharing it will be difficult. My apologies.
PS: The original excel file has too many sensitive data so sharing it will be difficult. My apologies.
Sub CopyData()
Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Dim length As Long
Dim counter As Integer
Dim vData As Variant
Dim objData As Object
Set objData = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
'If Len(Range("B2").Value) < 3 Then
' MsgBox "Please select CPE location", vbInformation, "Try Again"
' Exit Sub
If Len(Range("ACS!E2").Value) < 3 Or IsIPvalid(Range("ACS!E2").Value) = False Then
MsgBox "Please fill-in the IP field" & vbCr & "with a valid one", vbInformation, "Try Again"
Exit Sub
End If
Application.ScreenUpdating = False
On Error GoTo EndMacro:
With ActiveSheet.UsedRange
StartRow = .Cells(4, 1).Row
StartCol = .Cells(7).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
counter = 0
vData = ""
Application.Calculate
For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = "" 'Chr(34) & Chr(34)
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue ' & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
If Len(WholeLine) > 0 Then
If counter >= 1 Then
vData = vData & vbCr & vbLf
End If
'Debug.Print counter & " " & RowNdx
vData = vData & WholeLine & vbCr & vbLf
counter = 0
Else
counter = counter + 1
End If
Next RowNdx
With objData
.SetText vData
.PutInClipboard
End With
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Last edited: