gitmeto
Board Regular
- Joined
- Nov 24, 2021
- Messages
- 60
I have generated a code using the Macro recorder that works fine if it sits on its own. However, when I attempt to implement an event, it will not select the correct range. It seems as though that it is not seeing the range that I am attempting to copy data from. Any assistance would be greatly appreciated. It is erroring out on the line "Range("B3:B4").Select.
Rich (BB code):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lRow As Long
Dim KeyCells As Range
Dim strPath, strFile As String
lRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Assign variables for path and file
strPath = Range("C" & lRow).Value
strFile = Range("D" & lRow).Value
' The variable KeyCells contains the cells that will cause d' to be calculated when they are changed.
Set KeyCells = Range("D" & lRow)
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Workbooks.OpenText Filename:= _
strPath, Origin:= _
437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=False _
, Space:=False, Other:=True, OtherChar:=":", FieldInfo:=Array(Array(1, 1 _
), Array(2, 1)), TrailingMinusNumbers:=True
Range("B3:B4").Select
Selection.Copy
Windows("test.xlsm").Activate
Range("E" & lRow).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Windows(strFile).Activate
ActiveWindow.Close
MsgBox "Hello World"
End If
End Sub
Last edited by a moderator: