简体   繁体   English

比较B和C列,然后移动数据

[英]Compare Columns B and C then shift data

I need to compare Column B and C. They consist of real numbers only, sorted in ascending order. 我需要比较列B和列C。它们仅由实数组成,并按升序排列。

I already have the code for what I want it to do, but I need help changing the range in the code. 我已经有了所需的代码,但是我需要更改代码范围的帮助。

So currently code compares Column B and C. 因此,当前代码比较列B和列C。

查看此图像以获得更清晰的解释。

I need help changing code so I can add a column to the left of A and to the right of D also, and add those to shift range. 我需要更改代码的帮助,因此可以在A的左侧和D的右侧添加一列,并将其添加到平移范围。

Basically, compare E and F and shift B:E and F:I . 基本上,比较E和F并移位B:EF:I

Option Explicit
Sub AlignList()
' hiker95, 08/02/2012
' http://www.mrexcel.com/forum/showthread.php?651139-Compare-Column-B-amp-C-and-Shift
Dim r As Long, lr As Long, d As Range
Application.ScreenUpdating = False
lr = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
Set d = Range("A1:B" & lr)
r = 1
Do While d.Cells(r, 2) <> ""
  If d.Cells(r, 2).Offset(, 1) <> "" Then
    If d.Cells(r, 2) < d.Cells(r, 2).Offset(, 1) Then
      d.Cells(r, 2).Offset(, 1).Resize(, 2).Insert -4121
    ElseIf d.Cells(r, 2) > d.Cells(r, 2).Offset(, 1) Then
      d.Cells(r, 1).Resize(, 2).Insert -4121
      lr = lr + 1
      Set d = Range("A1:B" & lr)
    End If
  End If
  r = r + 1
Loop
Application.ScreenUpdating = 1
End Sub

You don't need a macro to do what you are trying to do (unless you are trying to do it automatically for some bizarre reason. 您不需要宏即可执行您要执行的操作(除非出于某些奇怪的原因而尝试自动执行该操作)。

You can just copy and paste columns D and C so that they are below the other data(see picture) and then add a 5th column that is the max of columns B and C(this will grab the number): 您可以复制并粘贴D和C列,使其位于其他数据下方(参见图片),然后添加第5列,该列是B和C列的最大值(这将获取数字):

之前回答

And then you just need to sort on the max column: 然后,您只需要对max列进行排序:

之后回答

EDIT: 编辑:

Ok so this is pretty basic excel so I am just going to post the screenshots with some small directions. 好的,这是一个非常基本的excel,因此我将发布带有一些小指示的屏幕截图。 Basically what you are trying to do(I don't know if you realize this) is combine two tables(granted it is in a really bizarre way). 基本上,您要尝试做的事情(我不知道您是否意识到了这一点)是将两个表组合在一起(当然,这确实是一种奇怪的方式)。 So if you follow my original instructions except use vlookup a little creatively you can absolutely accomplish what your are trying to do. 因此,如果您按照我的原始说明进行操作,除了创造性地使用vlookup之外,就可以完全完成您想做的事情。 Here are the screenshots: 这是屏幕截图:

So using vlookup creatively: 因此,创造性地使用vlookup: 原版的

You can match up the data that needs to be matched up: 您可以匹配需要匹配的数据: Vlookup1

And Here is the other vlookup column: 这是另一个vlookup列: Vlookup2

Then once your data is matched up - you can convert it all to values with copy and paste: 然后,一旦您的数据匹配-您可以通过复制和粘贴将其全部转换为值: 复制粘贴值

Then you add your max column: 然后添加您的最大列: 最大列

Then you just sort and you should get what you want: 然后,您只需排序即可获得所需的内容: 你想要什么

You can then delete any extra data that you don't need any more: 然后,您可以删除不需要的任何其他数据: DeleteAnythingYouDoNotWant

So hopefully that helps you out. 希望能对您有所帮助。 The only reason you would want to use VBA is if you want to automate this process - but if this is a one time thing then that would be overly time consuming. 您要使用VBA的唯一原因是要自动化该过程-但是,如果这是一次性的事情,那将是非常耗时的。 Excel is meant to manipulate data - match up tables - things like that - use it for that purpose. Excel旨在处理数据-匹配表-之类的东西-为此目的使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两列 A 和 B 以获取 3 列 C、D 和 E 中的数据 - Compare two columns A and B to get data in 3 columns, C,D & E 比较两个Excel列A&B,并在一个单元格中添加A中不存在的条目的第三列C的数据 - Compare two Excel columns A&B and add in one cell the data of a third column C of the entries not present in A 我有2列A和B我需要比较B列数据和A而不管所有行 - I have 2 columns A & B i need compare B column data with A irrespective of all rows Excel-如何比较2列中的单元格,如果B列匹配,则使用B列中匹配单元格旁边的C列中的值? - Excel - How to Compare Cells from 2 Columns then if Column B Matches, Use the Value in Column C next to the Matching Cell in Column B? 我想比较3个不同的列(B,C,D)的值,然后想在F列中获得较高的值结果 - I want to compare 3 different columns (B,C,D) values then want to get the higher value result in F column 将数据从B,C,D列移到A列-Excel - Move data from columns B,C,D.. to column A - excel 比较Excel中A和B列中的标点符号(VBA?) - Compare end punctuation in columns A and B in Excel (VBA?) 这是什么意思?:Columns(“ B:IV”)。删除Shift:= xlToLeft - What does this mean?: Columns(“B:IV”).Delete Shift:=xlToLeft VBA - 比较 2 列中的数据 - VBA - Compare data in 2 columns Excel公式将A列和B列的值转移到C和D空单元格 - Excel Formula to shift value of Column A and B to C and D empty cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM