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

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
142
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
 
It doesn't look like you gave access to anyone with the link. It is asking me to sign in.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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,215,069
Messages
6,122,954
Members
449,096
Latest member
Anshu121

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