+ Reply to Thread
Results 1 to 4 of 4

Tweak my code please to import excel sheets.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Tweak my code please to import excel sheets.

    I'm running into a little trouble with this code. I have commented the trouble spot.
    Cell "B4" would be populated from an input box.
    The variations in "B4" could be up to 8, but may be as few as 1.

    Sub Import_Files()
    
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim a, path1, zz, zzz As String
    Set ws1 = Worksheets("Lists")
    Set xl = CreateObject("Excel.Sheet")
    
    Application.ScreenUpdating = False
    path1 = ws1.Range("C4").Value
    a = ws1.Range("B4").Value
    
    do_over:
    CurrentFileName = Dir(path1 & "\" & a & "*.xls")
    
    Do
    'check for file name - delete if not current size listed in cell B4 on Lists sheet
    zz = Left(CurrentFileName, 4)
    zzz = ws1.Range("B4").Value
    If zz <> zzz Then GoTo skiptohere
    
      xl.Application.Workbooks.Open path1 & "\" & CurrentFileName
      Sheets().Move after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
      CurrentFileName = Dir()
      
    skiptohere:
    Loop While CurrentFileName <> ""
    
    '**********************************************************************
    ' Here is where I run into trouble.
    ' Cell "B4" would be like "W40X". (This is a file name "W40X210.xls")
    ' I need to also include workbooks named "WB40X", or "WS40X", etc.
    ' The file name could be up to 8 variations
    
    If dd = 1 Then GoTo put_them
    cc = Left(a, 1)
    If cc = "W" Then
      ee = "WB" & Right(a, 3)
      a = ee
      dd = 1
    End If
    
    If dd = 1 Then GoTo do_over
    '**********************************************************************
    
    put_them:
    dd = 0
    'Put each sheet rollweek in column "D" next to sheet name
    b = ws1.Range("D5").Value
    For a = 6 To b
      myname = ws1.Range("C" & a).Value
      Set ws2 = Worksheets(myname)
      ws1.Range("D" & a).Value = ws2.Range("B5").Value
    Next a
    End Sub
    Thanks

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Tweak my code please to import excel sheets.

    Hi,

    Perhaps

    ee = "WB" & Right(a, Len(a)-2)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: Tweak my code please to import excel sheets.

    I have to have the "40X" portion of the entry. I have played with wildcard such as " *40X* " and seem to run into problems. I'm sure it's just a syntax mistake, but can't seem to find it.

    Keep in mind that there may only be one file to import "W40X".

    Maybe I need some error checking...???



    'do you think this would work???
    '************************************************************************
    do_over:
    CurrentFileName = Dir(path1 & "\*" & a & "*.xls")
    '************************************************************************
    Adding wildcards around the filename selected ???

     Right(a, Len(a)-2)
    creates a problem because the filenames would be a list of different names like this...

    W40X120.xls
    W40X90.xls
    WB40X120.xls
    WS40X90.xls

    etc.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,963

    Re: Tweak my code please to import excel sheets.

    Dim XPos As Long, _
     Head As String, _
     Tail   As String
    
     XPos = InStr(Range("B4").Value, "x")
     Head = Left(Range("B4").Value, XPos)
     Tail = Right(Range("B4").Value,XPos+1,Len(Range("B4").Value)
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Best way to 'tweak' my code?
    By hutch@edge.net in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2008, 10:10 AM
  2. tweak code for excel 2007
    By Duncan1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-22-2007, 12:54 PM
  3. [SOLVED] Code Tweak
    By bodhisatvaofboogie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2006, 11:40 AM
  4. Newby Needs minor tweak on this VBA Macro code for Excel
    By zulfer7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2006, 12:35 AM
  5. excel code tweak for outlook - confusing
    By periro16 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-18-2005, 06:05 AM

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