Solved

VBA loop wipe 2 columns

Posted on 2011-09-15
9
420 Views
Last Modified: 2012-05-12
Hello everyone,
thanks to some of you I finally have my macro working, but I still have an issue when I run it it wipe out all the contents from B2,B3 and C2, C3 D2, D3 E2, E3, anyone knows why?
Public Function GetFileProperty(myFile As String, myType As String) As String

    'This creates an instance of the MS Scripting Runtime FileSystemObject class
    Set oFS = CreateObject("Scripting.FileSystemObject")

    If oFS.FileExists(myFile) Then
        Select Case UCase(Trim(myType))
            Case "CREATED"
                GetFileProperty = oFS.GetFile(myFile).DateCreated
            Case "MODIFIED"
                GetFileProperty = oFS.GetFile(myFile).DateLastModified
            Case "ACCESSED"
                GetFileProperty = oFS.GetFile(myFile).DateLastAccessed
            Case "SIZE"
                GetFileProperty = oFS.GetFile(myFile).Size
            'Case Else
                'GetFileProperty = "txt"
        End Select
    Else
        GetFileProperty = "File does not exist"
    End If
End Function

Public Sub SperiamoLoop()
    Dim MyCell As Variant, Rng As Range
    Dim myFile As String, myType As String
    Set Rng = Sheets("Sheet1").Range("A2:A4")
    For Each MyCell In Rng
        myFile = MyCell.Value
        If myFile <> "" Then
            For i = 1 To 4
                myType = Sheets("Sheet1").Range("A1").Offset(0, i)
                MyCell.Offset(0, i) = GetFileProperty(myFile, myType)
            Next
        End If
    Next
End Sub

Open in new window

z1.jpg
0
Comment
Question by:nassio1985
  • 4
  • 3
  • 2
9 Comments
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 36545536
Hello,
Make sure you correctly copy formula into cells.
Select B1 then copy it to B2, B3. If you click on B2 you must see your formula referenced to B2 and so on...

-FA
0
 

Author Comment

by:nassio1985
ID: 36545611
I've done this but every time I make the loop run it wipe the cell as you see in the screen shot.
0
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 36545757
Hello,
Select B1. Then Drag its right-down corner to B2, B3

or

Use Edit > Fill > Down command from menu.

-FA
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 500 total points
ID: 36545861
It didn't wipe the columns, it's just running the else every time again and you commented it out.

For your code to work as written, the first row MUST match your cases.
Make row 1:
Filename CREATED MODIFIED ACCESSED SIZE
Then DO NOT change it and the code will work.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 500 total points
ID: 36545901
If you want the columns named differently or arranged differently, then just make sure the column name matches the name in the CASE line.

If you want that data in different columns, then change this line
 For i = 1 To 4
If you change it to
 For i = 3 To 6
then it will use columns D:G instead of B:E
0
 

Author Comment

by:nassio1985
ID: 36546087
How could e running the else if like you said I commented it out, by the way I tried without comment and as you can see from the screen shot that's what I get, the first row does match my case as you said and I did it even before. How could I avoid the else to run again and again?
without-comments.jpg
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 500 total points
ID: 36546189
No, the text in the first row must exactly match the text in the cases. So B1 must contain the text CREATE spelled exactly the way that it is spelled in the case.
0
 

Author Comment

by:nassio1985
ID: 36546263
Hum... If I well understood this is what I sould put inside the B1 =GetFileProperty(A1,"CREATED"), if so it's not working....
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 36546726
No. In B1 just put CREATED
That's it. Just the text.
No = or formula or anything.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
A short article about a problem I had getting the GPS LocationListener working.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now