Good morning friends
How can we find the last active range? so the story: The range is dynamic ,,, example: sometimes range ("A2: J2") and sometimes range ("A2: AB2") how to fix this code ?
For Each rng In wbk.Sheets(3).Range("A2:J2") '<<< dynamic range "" ????
this is my code full
Sub try()
Dim fDialog As fileDialog
Dim wbk, Mywbk As Workbook
Dim rng As Range
Dim a As Variant
Dim i, ii, c, r, x, y, z
Set Mywbk = ActiveWorkbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Set fDialog = Application.fileDialog(msoFileDialogFilePicker)
With fDialog
If .Show = True Then
Dim fPath As Variant
fPath = .SelectedItems.Item(1)
Set wbk = Workbooks.Open(Filename:=fPath)
Else
MsgBox "blank"
Exit Sub
End If
End With
Mywbk.Activate
a = Mywbk.Sheets("Sheet1").UsedRange
With CreateObject("scripting.dictionary")
For i = 1 To UBound(a, 2)
If Not .exists(a(2, i)) Then
x = ""
For ii = 4 To UBound(a)
x = x & a(ii, i) & Chr(2)
Next
.Add a(2, i), x
End If
Next
For Each rng In wbk.Sheets(3).Range("A2:J2") '<<< dynamic range
c = rng.Column: r = rng.Row
y = rng.Value
x = .Item(y)
x = Split(x, Chr(2))
wbk.Sheets(3).Cells(r, c).Offset(1, 0).Resize(UBound(x)) = Application.Transpose(x)
Next
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub