Excel countIf +连接函数

时间:2016-01-29 07:18:22

标签: excel vba excel-formula

我有一个excel公式,只做一个简单的计数。但是,我想添加一个条件,如果遇到则会改变单元格的填充颜色。 例: 在Sheet 1单元格A1中,我有" XXX"。在Sheet 1单元格A2中,我的布尔值为" True"。在Sheet 2单元格B1中,我有

    =COUNTIF(A1,"*XXX*")

我想要做的是,如果在Sheet 1单元格A1中我有XXX和Sheet 1单元格A2是" True"然后在Sheet 2单元格B1中我应该有" 1"显示在红色单元格内

颜色编码的替代方案是,如果Sheet 1单元格A2的布尔值为" True"和表1单元格A1有XXX然后在表2单元格B1上,我应该有" 1R"显示。

感谢任何帮助

3 个答案:

答案 0 :(得分:1)

如果你想用VBA做,这段代码可以做你想要的:

Option Explicit
Sub test()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lastrow As Long
Dim i As Long

Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1") 'Change the name of your sheet
Set ws2 = wb.Sheets("Sheet2") 'Change the name of your sheet

Lastrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To Lastrow Step 2

    If ws1.Cells(i, "A") = "SEC" And ws1.Cells(i + 1, "A") = True Then
        ws2.Cells(i, "B") = 1
        ws2.Cells(i, "B").Interior.Color = RGB(255, 0, 0)
'       ws2.Cells(i, "B") = "1R" ' Alternate to the color coding
    End If

Next i

End Sub

修改

基于以下内容的代码

Option Explicit
Sub test()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lastrow As Long
Dim i As Long, c As Long

Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1") 'Change the name of your sheet
Set ws2 = wb.Sheets("Sheet2") 'Change the name of your sheet

i = 3
    For c = 7 To 25


        If ws2.Cells(i, "K") > 0 And ws2.Cells(i, "L") = False Then
            ws1.Cells(3, c).Interior.Color = RGB(255, 0, 0)

        ElseIf ws2.Cells(i, "K") > 0 And ws2.Cells(i, "L") = True Then
            ws1.Cells(3, c).Interior.Color = RGB(0, 255, 0)
        End If
i = i + 2

Next c


End Sub

答案 1 :(得分:0)

我认为最简单的方法是使用条件格式,您可以在开始>样式找到它。

  1. 选择需要更改颜色的单元格
  2. 添加新规则
  3. 使其符合公式
  4. 使用公式=(Sheet1!A2=TRUE)
  5. 格式化下面的示例单元格以符合您的要求
  6. 为您想要的每种颜色添加新规则(例如,如果您为FALSE项目选择绿色,则为

答案 2 :(得分:0)

或者您可以说:/* * Compile me with: * gcc -o dia dia.c `pkg-config --libs --cflags gtk+-2.0 goocanvas` -rdynamic -I./headers/ -lm * */ #include <stdio.h> #include <gtk/gtk.h> #include <goocanvas.h> /*Common structs and varaibles*/ struct dia_struct { gdouble diam; const gchar *tbp, *thtp, *wt; }; struct dia_struct set_dia; /* intitializing the structure */ /* Goo canvas items */ GooCanvasItem *root, *path, *path2; GtkWidget *canvas; /* * Function: ofdra * -------------------- * This is the main function to draw the diagram on screen * * returns: none */ void ofdra (double diam) { double dfactor = diam*50; path = NULL; path2 = NULL; double x = (465-dfactor)/2; double y = 465-x; char buf[100]; char buf2[100]; sprintf(buf, "M 0 %f L 1000 %f", x, x); sprintf(buf2, "M 0 %f L 1000 %f", y, y); path = goo_canvas_path_new (root, buf, "stroke-color", "red", NULL); path2 = goo_canvas_path_new (root, buf2, "stroke-color", "red", NULL); /*goo_canvas_item_update (path, TRUE, );*/ /*goo_canvas_item_update (path2);*/ } /* * Function: ofruta * -------------------- * This is the main function to run when in a user changed a value. * * glade_wdgets: widget-object * pObList: array of objects to go through * * returns: none */ void ofruta (GtkWidget *glade_wdgets, gpointer *pObList) { /* common varaibles needed */ struct dia_struct dia; /* intitializing the structure */ /* getters */ dia.diam = gtk_spin_button_get_value (GTK_SPIN_BUTTON(pObList[0])); /* diameter reading */ set_diamond.diam = diamond.diam; gtk_spin_button_set_value(GTK_SPIN_BUTTON(pObList[0]), set_diamond.diam); /* Loads all dynamically generated diagram drawing */ ofdra(set_diamond.diam); } //=========================================================================== /* * main * * Program begins here */ int main( int argc, char **argv ) { GtkBuilder *builder; GtkWidget *window; GError *error = NULL; GtkButton *button; GtkLabel *label; cairo_surface_t *surface; /* Init GTK+ */ gtk_init( &argc, &argv ); /* Create new GtkBuilder object */ builder = gtk_builder_new(); /* Load UI from file. If error occurs, report it and quit application. * Replace "tut.glade" with your saved project. */ if( ! gtk_builder_add_from_file( builder, "dia_glade.glade", &error ) ) { g_warning( "%s", error->message ); g_free( error ); return( 1 ); } /* Get main window pointer from UI */ window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) ); gpointer spinners[] = { gtk_builder_get_object( builder, "diam" )}; /* Connect signals */ gtk_builder_connect_signals( builder, spinners ); g_signal_connect(G_OBJECT(window), "delete-event", (GCallback)gtk_main_quit, NULL); g_signal_connect(G_OBJECT("measure"), "clicked", (GCallback)ofruta, spinners); canvas = goo_canvas_new (); gtk_widget_set_size_request (canvas, 600, 465); goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 1000, 1000); gtk_widget_show (canvas); gtk_container_add (GTK_CONTAINER (gtk_builder_get_object( builder, "draw_area" )), canvas); root = goo_canvas_get_root_item (GOO_CANVAS (canvas)); /* Destroy builder, since we don't need it anymore */ g_object_unref( G_OBJECT( builder ) ); /* Show window. All other widgets are automatically shown by GtkBuilder */ gtk_widget_show( window ); /* Start main loop */ gtk_main(); return( 0 ); } 这只会返回1,或者你可以修改为“1R”,如果a1和A2都返回你输入和声明的测试。