+ Reply to Thread
Results 1 to 21 of 21

Code to write numbers from one cell to another

Hybrid View

  1. #1
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Code to write numbers from one cell to another

    I was looking for help to develop some VBA code to write from potentially 4 cells to a possible 4 cells. I have uploaded my sample Excel sheet image with the expected result. If someone could kindly assist.

    The macro would need to start with searching cells B3, B4, E3, E4 to determine if any numbers are present. When found use that number (example cell B3 has a 7 in it) to search for the same number in cells A14, E14, I14, L14 (cell E14 has the 7). When found write the contents from cell C3 (the adjacent cell to the right of the 7 in this example) to cell F14 and change the last number to a 1 (so cell F14 would be written as 8-1). Repeat for each cell B3, B4, E3, E4 and afterwards delete all the contents in B3,C3, B4,C4, E3,F3, E4,F4.

    Thanks in advance for your help.

    EXCEL 5.JPEG

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Code to write numbers from one cell to another

    You must learn to attach a spreadsheet rather than an image.


    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.



    
    Sub test()
    'Searching cells B3, B4, E3, E4 to determine if any numbers are present.
    ''
    SearchA = Split("B3, B4, E3, E4", ", ")
    TargetA = Split("A14, E14, I14, L14", ", ")
    
    For Count = 0 To 3
    If IsNumeric(Range(SearchA(Count)).Value) Then
    
    'Search for the same number in cells A14, E14, I14, L14
    For Count2 = 0 To 3
    If Range(SearchA(Count)).Value = Range(TargetA(Count2)).Value Then
    T = Split(Range(SearchA(Count)).Offset(0, 1).Value, ".")
    Range(TargetA(Count2)).Offset(0, 1).Value = T(0) & ".1"
    End If
    Next
    
    
    End If
    Next
    
    End Sub
    Last edited by mehmetcik; 11-27-2018 at 03:33 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Works but need to make 2 small changes:

    In the Excel example in cell F14 the write is 8-15.1 (should be 8-1) and the cell contents in B3,C3, B4,C4, E3,F3, E4,F4 are not being deleted. I have uploaded my Excel sheet and will so in the future.

    Thanks so much for your help.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Are you still there?

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Code to write numbers from one cell to another

    Try
    Sub twst()
    Dim r as range,t,dic as object,c as range
    Set r =[b3:f4]
    Set dic = CreateObject("Scripting.dictionary")
    For each c in r
     If isnumeric(c.value) = true then
        t = split(c.offset(,1).value,"-")
        dic(c.value) = t(0) & "-" & 1
     End if
    Next c
    r.clear
    For each c in rows(14).specialCells(2,1)
      If dic.exists(c.value) then
        c.offset(,1).value = dic(c.value)
      End if
    Next c
    End sub
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  6. #6
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Code to write numbers from one cell to another

    try this work
    Sub twst()
    Dim r As Range, t, dic As Object, c As Range
    Set r = [b3:f4]
    Set dic = CreateObject("Scripting.dictionary")
    For Each c In r
     If IsNumeric(c.Value) * Len(c.Value) = True Then
        t = Split(c.Offset(, 1).Value, "-")
        dic(c.Value) = "'" & t(0) & "-" & 1
     End If
    Next c
    ' r.Clear
    For Each c In Rows(14).SpecialCells(2, 1)
      If dic.exists(c.Value) Then
        c.Offset(, 1).Value = dic(c.Value)
      End If
    Next c
    End Sub

  7. #7
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Hi, it says compile error, Sub or Function not defined and it highlights “isnumeric”.

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Code to write numbers from one cell to another

    
    Sub test()
    'Searching cells B3, B4, E3, E4 to determine if any numbers are present.
    ''
    SearchA = Split("B3, B4, E3, E4", ", ")
    TargetA = Split("A6, E6, I6, L6", ", ")
    
    For Count = 0 To 3
    If IsNumeric(Range(SearchA(Count)).Value) Then
    
    'Search for the same number in cells A14, E14, I14, L14
    For Count2 = 0 To 3
    If Range(SearchA(Count)).Value = Range(TargetA(Count2)).Value * 1 Then
    T = Split(Range(SearchA(Count)).Offset(0, 1).Value, "-")
    Range(TargetA(Count2)).Offset(0, 1).Value = T(0) & ".1"
    End If
    Next
    
    End If
    Next
    
    For Count = 0 To 3
    Range(SearchA(Count)).Resize(1, 2).Value = ""
    Next
    
    End Sub

  9. #9
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Works great!

    One problem though. How do I expand the
    “TargetA = Split("A6, E6, I6, L6", ", ") line to include more cells and do I have make changes to another line also? I put more cells in followed by a comma but nothing. Nice to see it work though.

    Thank-you for your help.

  10. #10
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Hi

    Not sure if you received my previous note.
    Thanks

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Code to write numbers from one cell to another

    Don't hardcode the loop boundaries. Use:

    For Count = LBound(SearchA) To UBound(SearchA)
    and:

    For Count2 = LBound(TargetA) To UBound(TargetA)
    Everyone who confuses correlation and causation ends up dead.

  12. #12
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Hi rorya

    Thanks so much for your response but I have tried putting Ubound(SearchA) in a few places but I don’t think I’m getting it. If you have a minute could you please copy the code to where it belongs. I think this could be a blond moment?

  13. #13
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Code to write numbers from one cell to another

    what additional cells are you talking about?

  14. #14
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Code to write numbers from one cell to another

    my 2 cents:

    Sub scott1945()
    
    Dim srng As Range
    Dim drng As Range
    Dim sVal As Variant, fVal As Variant
    
    Set srng = Range("B3:B4, E3:E4")
    Set drng = Range("A14, E14, I14, L14")
    
    For Each sVal In srng
        If sVal.Value <> "" Then
            Set fVal = drng.Find(sVal, lookat:=xlWhole)
                If Not fVal Is Nothing Then
                    fVal.Offset(, 1) = Left(sVal.Offset(, 1), InStr(sVal.Offset(, 1), "-")) & "1"
                End If
        End If
    Next
    End Sub
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  15. #15
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Hi everyone

    Thank-you to all 3 of you for your help.
    mehmetcik - your question "what additional cells are you talking about?" is just wanting to add some additional cells to your original code. Your code works perfectly well with the original supplied cells if I want to add some more but I couldn't.

    bulina2k - Your code works as well but I need to also delete the contents in cells "B3:B4, E3:E4" after the write.

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Code to write numbers from one cell to another

    Try
    Sub test()
        Dim myAreas As Areas, myArea As Range, i As Long
        Dim r As Range, ff As String
        Set myAreas = Rows(3).SpecialCells(2).Areas
        For Each myArea In myAreas
            For i = 1 To myArea.CurrentRegion.Rows.Count
                Set r = Rows(6).Find(myArea(i, 1).Value, , , 1)
                If Not r Is Nothing Then
                    ff = r.Address
                    Do
                        r(, 2).Value = "'" & Val(myArea(i, 2).Value) & "-1"
                        Set r = Rows(6).FindNext(r)
                    Loop While ff <> r.Address
                End If
            Next
            myArea.CurrentRegion.ClearContents
        Next
    End Sub

  17. #17
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Thank-you for your response jindon, I will test out your code.

  18. #18
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    bulina2k I have added srng.ClearContents at the end of the code which works but how do I clear the contents in cells C3:C4 and F3:F4?

    Next
    srng.ClearContents
    End Sub

  19. #19
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Code to write numbers from one cell to another

    like this
    Next
    srng.ClearContents
    srng.offset(,1).ClearContents
    End Sub

  20. #20
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Hi jindon

    The big issue is that this is hard coded and my sample is just that only a sample. This code will not work in the sheet that I use. I will use the code from bulina2k but I would like to thank-you for your help.

  21. #21
    Registered User
    Join Date
    09-09-2018
    Location
    Vancouver, canada
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to write numbers from one cell to another

    Hi bulina2k

    Your code works so well. A huge Thank-you for all your help!!!!!

+ 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. Find cell with text, offset to the left one cell and write code
    By timas in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-23-2015, 01:22 AM
  2. how can i write vba code for replacing continuos fraction numbers in on row
    By baig123 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-21-2014, 08:04 AM
  3. Code to write to a cell on another sheet
    By neil40 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-23-2013, 04:31 AM
  4. [SOLVED] Code to evaluate contents of one cell then write new values into new cell (Simple IF Stmt)
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2012, 11:54 AM
  5. Write the numbers into one cell
    By kapeller in forum Excel General
    Replies: 0
    Last Post: 02-12-2011, 02:19 AM
  6. Replies: 6
    Last Post: 01-13-2010, 10:26 AM
  7. Replies: 3
    Last Post: 01-10-2005, 08:06 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