Dropdownlist loop copy paste

rizabdullah

New Member
Joined
Mar 13, 2015
Messages
6
Hi Guys

I am a beginner with vb and wondering if somebody can help.

I have source workbook with dropdowllist in B3 sheet name "Front Sheet", the list is based on data from another sheet in same workbook. I also have destination macro-enabled workbook "Combined", from here I like to run my vb.

What I am trying to achieve is to be able to loop through the DDL in source file front sheet, read the first code from DDL set that as range, update the data (data range B1:K50) in front sheet, copy this range and paste it to the Combined workbook on new sheet fronm A1 and rename this new sheet as the value from the source file Front Sheet B3 value.

I have managed the below below which runs, HOWEVER, it seems like its not changing the code in B3 hence not updating the range on Front Sheet workbook. Please can someone help:

Sub mytset()

Dim SourceWB As Workbook, DestWB As Workbook
Dim SourceSht As Worksheet, DestSht As Worksheet
Dim c As Range, myListRng As Range
Dim myListStr As String, myShtStr As String, myRngStr As String

' Initial
Set SourceWB = Workbooks("Refurbs Tracker") ' <~~ Use your Source Workbook name - "Refurbs Tracker" ?
Set DestWB = Workbooks("Combined") ' <~~ Use your Destination Workbook name - "Combined" ?
Set SourceSht = SourceWB.Worksheets("Front Sheet") ' <~~ Use your Source Sheet name - "Front Sheet" ?

' find the drop down values
If SourceSht.Range("B3").Validation.Type = xlValidateList Then
myListStr = Mid(SourceSht.Range("B3").Validation.Formula1, 2)
On Error Resume Next
Set myListRng = SourceWB.Names(myListStr).RefersToRange
If Err.Number <> 0 Then
myShtStr = Left(myListStr, InStr(1, myListStr, "!") - 1)
myRngStr = Right(myListStr, Len(myListStr) - Len(myShtStr) - 1)
myShtStr = Replace(myShtStr, "'", "")
Set myListRng = SourceWB.Worksheets(myShtStr).Range(myRngStr)
End If
On Error GoTo 0
Else
MsgBox "Problem with Validation List"
Exit Sub
End If

' loop through the drop down values and do work
For Each c In myListRng
If SheetExists(c.Value, DestWB) Then
Set DestSht = DestWB.Worksheets(c.Value)
Else
Set DestSht = DestWB.Worksheets.Add
DestSht.Name = c.Value
End If
SourceSht.Range("B1:K50").Copy
DestSht.Range("A1").PasteSpecial PASTE:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next c
Application.CutCopyMode = False

' Clean up
Set SourceSht = Nothing
Set DestSht = Nothing
Set SourceWB = Nothing
Set DestWB = Nothing

End Sub




Function SheetExists(Name As String, WB As Workbook) As Boolean
Dim WS As Worksheet
SheetExists = False
For Each WS In WB.Worksheets
If Name = WS.Name Then
SheetExists = True
GoTo CleanUp:
End If
Next WS
CleanUp:
Set WS = Nothing
End Function
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top