Consulting

Results 1 to 5 of 5

Thread: Solved: macro help

  1. #1
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location

    Solved: macro help

    Hi

    i need help with some macro.
    i have 2 columns.
    What i need this if the cell in column "D" is empty copy the contaimnet of the cell in column "B" on the same row and if both of the cell is empty live it empty!

    Thanks

  2. #2
    Hi dude,

    check this code:

    Sub test()
    Dim i As Integer, lr As Integer
    
    lr = ActiveSheet.UsedRange.Rows.Count
    
    For i = 1 To lr
    
        If Range("D" & i).Value = "" Then
        Range("D" & i).Value = Range("B" & i).Value
        End If
        
        Next
        
    End Sub
    Keep Smiling

  3. #3
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    try that mate:


    [VBA]
    Sub tryme()

    Dim lr As Long, i As Long

    With Worksheets("YourWorksheetName")
    lr = .Range("B" & .Rows.Count).End(xlUp).Row

    For i = 2 To lr
    If .Cells(i, "D").Value = "" And .Cells(i, "B").Value <> "" Then
    .Cells(i, "D").Value = .Cells(i, "B").Value
    End If
    Next i

    End With

    End Sub
    [/VBA]

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can skip checking evey cell in the range
    [VBA]
    Dim Rng As Range, cel As Range
    Set Rng = Range(Cells(1, 4), Cells(Rows.Count, 4).End(xlUp))
    Set Rng = Rng.SpecialCells(xlCellTypeBlanks)
    For Each cel In Rng
    cel = cel.Offset(, -2)
    Next

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    And one more one
    
    Sub Test1()
      Const StartRow = 2  ' <-- Change to suit
      With ActiveSheet.UsedRange
        With Range(Cells(StartRow, "D"), Cells(.Row + .Rows.Count - 1, "D"))
          .Value = .Offset(, -2).Value
        End With
      End With
    End Sub
    P.S. Not sure that I’ve understood the task correctly, Oleg if it's more suitable you can ask me by PM on Russian
    Last edited by ZVI; 01-30-2010 at 11:29 PM.

Posting Permissions

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