我正在尝试将相邻行中的单元格合并到已合并的单元格中。如果合并D列中的相邻单元格,我想合并列C中的相邻单元格。并非所有单元格都在D列中合并。下面的代码在我使用MsgBox中的变量时为我提供了正确的行号,但是,当我将变量添加到要合并的Range时,每行都会合并。我认为它必须是一些简单的,但我可以确定是什么导致范围中的每一行合并。我通常不会混淆任何东西,但我需要将这些合并的单元格留在电子表格中。非常感谢你帮助破解这个。
Sub FindMerge()
Dim cell As Range
Dim Lrow As Long
Application.ScreenUpdating = False 'Turn off screen updating. Code runs faster without screen flicker
Application.DisplayAlerts = False 'stops Windows Alerts from poping up
'loops through range to find merged cells
'For Each cell In ActiveSheet.UsedRange 'commented out to try static range below.
For Each cell In Range("D1:D81")
If cell.MergeCells Then
If cell.Address = cell.MergeArea.Cells(1, 1).Address Then
' Msgbox "Row: " & cell.row 'displays correct row number where merged cell is located
Lrow = cell.row
Range("C2:O" & Lrow).Merge True 'Unintentionally merges every row
End If
End If
Next cell
Application.ScreenUpdating = True 'Turns screen updating back on
Application.DisplayAlerts = True 'Turns Windows Alerts back on
End Sub
答案 0 :(得分:0)
您正在合并从第2行开始的所有行:
import java.util.Scanner; import java.util.Random; public class testcar { public static void main(String[] args) { int repeat = 1; boolean ignitionStat = false; while (repeat == 1) { System.out.println("What would you like to do?"); System.out.println("1: turn the ignition on/off"); System.out.println("2: change the position of car"); System.out.println("q: quit this program "); Scanner sc = new Scanner(System.in); Scanner ex = new Scanner(System.in); String i = sc.nextLine(); switch (i) { case "1": System.out.println("You chose to turn on/off the ignition"); ignitionSwitch(ignitionStat); System.out.println("CAR INFORMATION"); System.out.println("IGNITION STATUS: "); reportState(); repeat = 1; break; case "2": System.out.println("THIS WILL BE CHANGE POSITION METHOD"); repeat = 2; break; case "q": System.out.println("You choose to quit"); repeat = 0; break; default: System.out.print("INVALID OPTION"); break; } } while (repeat == 2){ System.out.println ("In which direction do you want to move the car? "); Scanner hv= new Scanner (System.in); String z = hv.nextLine(); switch (z) { case "h": System.out.println("You will move the car horizontally!"); repeat = 0; break; case "v": System.out.println("You will move the car vertically!"); repeat = 0; break; default: System.out.println("Invalid Option!"); repeat = 2; break; } } } public static boolean ignitionSwitch(boolean ignitionStat){ return ignitionStat =! ignitionStat; } public static void reportState(){ int positionX = randomizePosition(); int positionY = randomizePosition(); char carColor = assignColor(); System.out.println ("CAR INFORMATION"); System.out.println ("Color : " + carColor); System.out.println ("Location:" + "(" +positionX +","+positionY +")"); String grid = "--------------------"; for (int i =1; i<21; i++){ if (i != positionY){ System.out.println(grid); } else if (i == positionY){ String newgrid = grid.substring(0,positionX-1) + carColor + grid.substring(0,20-positionX); System.out.println(newgrid); } } } public static int randomizePosition () { Random rand = new Random(); int position = rand.nextInt(20) + 1; return position; } public static char assignColor () { Random rand = new Random(); int colorNum = rand.nextInt(5) + 1; char colorStr; switch (colorNum) { case 1: colorStr = 'G'; break; case 2: colorStr = 'B'; break; case 3: colorStr = 'W'; break; case 4: colorStr = 'S'; break; default : colorStr = 'R'; break; } return (colorStr); } }
你最想要的是:
Range("C2:O" & Lrow).Merge