Results 1 to 2 of 2

How to properly loop a subroutine with multiple dim variables

Threaded View

  1. #1
    Registered User
    Join Date
    06-25-2013
    Location
    Chicago, IL
    MS-Off Ver
    Office 365
    Posts
    6

    How to properly loop a subroutine with multiple dim variables

    Hello-

    I cobbled together this code to copy data from another opened workbook and paste it onto a different workbook exactly where I want it to. It works great but I am having a hard time figuring out how to repeat (maybe loop) it without simply pasting it 20 times and manually updating it.
    I need the subroutine to loop and instead of using my dimensional variable "Hilton1" to use "Hilton2", and then use "Hilton3" and so on. Thanks for your time.

    Sub AutoImportHilton()
    
    Dim w As Workbook
    Dim wkboriginal As Excel.Workbook
    Dim Hilton1 As Range
    Dim Hilton2 As Range
    Dim Hilton3 As Range
    Dim rng As Range
    
    InputDate = InputBox("Enter OTB Import Date MM-DD-YY")
    Set wkboriginal = ActiveWorkbook
    Set Hilton1 = Sheets("AutoImport").Range("B8")
    Set Hilton2 = Sheets("AutoImport").Range("B9")
    Set Hilton3 = Sheets("AutoImport").Range("B10")
    
    For Each w In Application.Workbooks
          If (w.Name) Like "*" & Hilton1 & "*" Then
         Exit For
      End If
    Next w
    If Not w Is Nothing Then
    w.Activate
    Else
        MsgBox "No " & Hilton1 & " workbook found!"
    End If
    Range("D7", Range("D6").End(xlDown)).Select
        Selection.ClearFormats
        Selection.NumberFormat = "0"
        Selection.HorizontalAlignment = xlCenter
        Selection.Copy
    wkboriginal.Activate
    Worksheets("Input" & Hilton1).Select
            Set rng = Cells.Find(What:="otb " & InputDate, LookIn:=xlValues)
            If Not rng Is Nothing Then
                rng.Activate
            Else
                MsgBox ("Date already used or not a weekday date")
                Exit Sub
            End If
    Selection.PasteSpecial
    
    End Sub
    Last edited by qualk23; 07-10-2020 at 01:56 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Nested Do Until loop with multiple variables
    By tonypizza in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-30-2019, 02:38 PM
  2. [SOLVED] How to access variables that are declared in private subroutine
    By Pan314 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-23-2015, 10:07 PM
  3. [SOLVED] Multiple Variables in For Each Loop
    By Excel Guy 123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2014, 11:47 PM
  4. passing variables to subroutine from userform
    By PD3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-07-2013, 02:11 AM
  5. [SOLVED] calling Subroutine is not working properly
    By kiran527 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-05-2012, 01:24 AM
  6. Moving from For...Next loop to subroutine and back to loop
    By zabrahamson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2011, 01:26 PM
  7. Subroutine question and public variables
    By musictech in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2005, 05:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1