Consulting

Results 1 to 15 of 15

Thread: VBA- Variable declaration help

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    VBA- Variable declaration help

    Hi Team,


    'Help1 - Need help in declaring variable for Application.worksheetfunction.


    Dim wsf As object or Variant
    Set wsf = Application.WorksheetFunction


    With wsf
    wsf. 'not getting help after pressing wsf.
    end with


    Help2 - Can we shorten the code below in a single line


    Range("c3:c8").value ="-"
    Range("e3:e8").value ="-"
    Range("g3:g8").value ="-"
    Range("I3:I8").value ="-"
    Range("K3:K8").value ="-"
    Range("M3:M8").value ="-"
    Range("N3:N8").value ="-"and so on till Till Range("v3:v8")


    Thanks for your help in advance!




    Regards
    mg

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
        Dim wsf As WorksheetFunction
    Use two nested loops, one for 3 to 8 and one for c to v.
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Dim wsf As WorksheetFunction
    Range("C3:C8,E3:E8,G3:G8,...").Value = "-"
    Artik

  4. #4
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location
    Hi Artik and Paulked.

    Thanks for your help its working.


    Regards,
    mg

  5. #5
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Sub test()
        Dim rw As Long, cl As Long
        For rw = 3 To 8
            For cl = 3 To 22
                Cells(rw, cl) = "-"
            Next
        Next
    End Sub
    Semper in excretia sumus; solum profundum variat.

  6. #6
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    If the spacing between ranges is not regular and you want to do it in a loop, you can do it this way:
    Sub AAA()
      Dim i As Long
      Dim varArr As Variant
      
      varArr = Split("C3,E3,G3,I3,K3,M3,N3,Q3,R3,V3", ",")
      
      For i = 0 To UBound(varArr)
          Range(varArr(i)).Resize(5).Value = "-"
      Next i
      
    End Sub
    Artik

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    and so on till Till Range("v3:v8")
    Well... That's a broken pattern, so there is no "and so on."

    Function MeLazy() AS Boolean
         MeLazy = PauldKed + Artik
    End Function
    Sub test()
        Dim cl As Long
    
    'Step 2 = every other column (up to, and including,  u)
        For cl = 3 To 21 Step 2
             Cells(3, cl).Resize(5).Text = "-"
       Next
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    I'd use another pejorative

    Sub M_snb()
       [C3:V8] = [if(mod(column(c3:V3),2)=1,"-",C3:V8)]
    End Sub

  10. #10
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    There's a break in the pattern -- is that correct?

    Range("c3:c8").value ="-"
    Range("e3:e8").value ="-"
    Range("g3:g8").value ="-"
    Range("I3:I8").value ="-"
    Range("K3:K8").value ="-"
    Range("M3:M8").value ="-"
    
    'pattern break
    
    Range("N3:N8").value ="-"and so on till Till Range("v3:v8")

    Part 1: C-E-G-I-K-M (from C up by 2 columns)

    Part 2: N-P-R-T-V (from N up by 2 columns)

    Option Explicit
    
    Sub test()
        Dim wsf As WorksheetFunction
        Dim c As Long
    
    
        Set wsf = Application.WorksheetFunction
    
    
        For c = 3 To 13 Step 2
            Cells(3, c).Resize(6, 1).Value = "-"
        Next c
        For c = 14 To 22 Step 2
            Cells(3, c).Resize(6, 1).Value = "-"
        Next c
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    "pejorative"?

  13. #13

  14. #14

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location

    Infinite Loop?

    "pejorative" --> "periorative" -->"pejorative" --> "periorative" --> "pejorative" --> "periorative" --> ......
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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