How to copy data from single cells to merge cells and vice versa

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
152
Office Version
  1. 2021
Platform
  1. Windows
i have these codes which copy data from data sheet to quot sheet was working fine, but now my quot sheet has few merge cells now it dont copy. does merge cells dont copy values from single cells sheets?

VBA Code:
Sub search_quot()
    Dim Quot_No As String, Fnd As Range, Ws1 As Worksheet, Ws2 As Worksheet, FileNameRef As String, X As Long, Y As Long: Y = 17
    
    Sheets("Quot").Unprotect
    Application.ScreenUpdating = False
    
    Set Ws1 = Sheets("database")
    Set Ws2 = Sheets("QUOT")
    
    FileNameRef = Range("I3").Value
    
    Set Fnd = Ws1.Range("A:A").Find(Ws2.Range("I3").Value, , , xlWhole, , , , , False)
    If Fnd Is Nothing Then MsgBox "Quotation Number [-]> " & FileNameRef & " <[-]" & vbNewLine & "Not found in Quotation Database.", vbExclamation, "Quote Search ERROR": Exit Sub
    
    With Ws2
        .Range("I4").Value = Fnd.Offset(, 1).Value
        .Range("I5").Value = Fnd.Offset(, 128).Value
        .Range("C8").Value = Fnd.Offset(, 2).Value
        .Range("C9").Value = Fnd.Offset(, 3).Value
        .Range("A49").Value = Fnd.Offset(, 129).Value
        .Range("A51").Value = Fnd.Offset(, 130).Value
        .Range("H61").Value = Fnd.Offset(, 7).Value
             For X = 8 To 124 Step 4
            .Range("A" & Y).Resize(, 4).Value = Array(Fnd.Offset(, X).Value, Fnd.Offset(, X + 1).Value, Fnd.Offset(, X + 2).Value, Fnd.Offset(, X + 3).Value)
            Y = Y + 1
        Next X
    End With
     Range("A17").Select
     ActiveWindow.ScrollRow = 1
    'Sheets("Quot").Protect AllowFormattingCells:=True
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Using the code that is in the Oilfield workbook, can you try
replacing this:
VBA Code:
        For X = 8 To 124 Step 4
            For Each cell In .range("A" & Y & ":D" & Y).Cells
                cell.Value = Fnd.Offset(, X).Value
                If cell.MergeCells Then
                    ' Handle merged cells
                    cell.MergeCells = False
                    cell.MergeArea.Value = Fnd.Offset(, X).Value
                    cell.MergeCells = True
                End If
                X = X + 1
            Next cell
            Y = Y + 1
        Next X

With this
VBA Code:
        For X = 8 To 124 Step 4
            .Range("A" & Y).Value = Fnd.Offset(, X).Value
            .Range("B" & Y).Value = Fnd.Offset(, X + 1).Value
            .Range("G" & Y).Value = Fnd.Offset(, X + 2).Value
            .Range("H" & Y).Value = Fnd.Offset(, X + 3).Value
            Y = Y + 1
        Next X
 
Upvote 0
Solution
Using the code that is in the Oilfield workbook, can you try
replacing this:
VBA Code:
        For X = 8 To 124 Step 4
            For Each cell In .range("A" & Y & ":D" & Y).Cells
                cell.Value = Fnd.Offset(, X).Value
                If cell.MergeCells Then
                    ' Handle merged cells
                    cell.MergeCells = False
                    cell.MergeArea.Value = Fnd.Offset(, X).Value
                    cell.MergeCells = True
                End If
                X = X + 1
            Next cell
            Y = Y + 1
        Next X

With this
VBA Code:
        For X = 8 To 124 Step 4
            .Range("A" & Y).Value = Fnd.Offset(, X).Value
            .Range("B" & Y).Value = Fnd.Offset(, X + 1).Value
            .Range("G" & Y).Value = Fnd.Offset(, X + 2).Value
            .Range("H" & Y).Value = Fnd.Offset(, X + 3).Value
            Y = Y + 1
        Next X
thank you very much these code work, now qty and unit price get copy from database sheet back to quot sheet. if you downloaded my sample file, you can see if i save quot sheet data in database sheet it dont save qty and unit price in database, if you can do something about it. these are the codes

VBA Code:
i = 8
      For r = 1 To 30
         For j = 1 To 4
         C = Choose(j, 1, 2, 3, 4)
         Debug.Print .range("A17:A46,B17:F46,G17:G46,H20:H46").Cells(r, C).Address
            Rng.Offset(1, i).Value = .range("A17:A46,B17:F46,G17:G46,H20:H46").Cells(r, C)
            i = i + 1
         Next j
      Next r
 
Upvote 0
I am login off for tonight and will have a look tomorrow.
Can you confirm which sub we are talking about now ? Is it qaddata ? and is it meant to write from quote to the database ?
 
Upvote 0
I am login off for tonight and will have a look tomorrow.
Can you confirm which sub we are talking about now ? Is it qaddata ? and is it meant to write from quote to the database ?
ok thnx again
yes correct q_add_data which is meant to save data to database
 
Upvote 0
Try changing your Choose line to the one below:
VBA Code:
         C = Choose(j, 1, 2, 7, 8)                  ' Data columns are A, B, G, H
 
Upvote 0

Forum statistics

Threads
1,224,975
Messages
6,182,104
Members
453,088
Latest member
Chaoxite

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top